package sige

import org.springframework.dao.DataIntegrityViolationException

class OrdemController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    static permissoes = ["index", "list", "create", "save", "show", "edit", "update", "delete"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [ordemInstanceList: Ordem.list(params), ordemInstanceTotal: Ordem.count()]
    }

    def create() {
        [ordemInstance: new Ordem(params)]
    }

    def save() {
        def ordemInstance = new Ordem(params)
        if (!ordemInstance.save(flush: true)) {
            render(view: "create", model: [ordemInstance: ordemInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'ordem.label', default: 'Ordem'), ordemInstance.id])
        redirect(action: "show", id: ordemInstance.id)
    }

    def show(Long id) {
        def ordemInstance = Ordem.get(id)
        if (!ordemInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'ordem.label', default: 'Ordem'), id])
            redirect(action: "list")
            return
        }

        [ordemInstance: ordemInstance]
    }

    def edit(Long id) {
        def ordemInstance = Ordem.get(id)
        if (!ordemInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'ordem.label', default: 'Ordem'), id])
            redirect(action: "list")
            return
        }

        [ordemInstance: ordemInstance]
    }

    def update(Long id, Long version) {
        def ordemInstance = Ordem.get(id)
        if (!ordemInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'ordem.label', default: 'Ordem'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (ordemInstance.version > version) {
                ordemInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'ordem.label', default: 'Ordem')] as Object[],
                          "Another user has updated this Ordem while you were editing")
                render(view: "edit", model: [ordemInstance: ordemInstance])
                return
            }
        }

        ordemInstance.properties = params
        
        def ordemOld = Ordem.get(id)
        if( ordemInstance.movimentacoes && ordemInstance.comportamento != ordemOld.comportamento){
            ordemInstance.errors.rejectValue("comportamento", "default.uneditable.field.failure",
                          [message(code: 'ordem.label', default: 'Ordem')] as Object[],
                          "O campo comportamento não pode ser alterado")
            render(view: "edit", model: [ordemInstance: ordemInstance])
            return
        }

        if (!ordemInstance.save(flush: true)) {
            render(view: "edit", model: [ordemInstance: ordemInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'ordem.label', default: 'Ordem'), ordemInstance.id])
        redirect(action: "show", id: ordemInstance.id)
    }

    def delete(Long id) {
        def ordemInstance = Ordem.get(id)
        if (!ordemInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'ordem.label', default: 'Ordem'), id])
            redirect(action: "list")
            return
        }

        try {
            ordemInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'ordem.label', default: 'Ordem'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'ordem.label', default: 'Ordem'), id])
            redirect(action: "show", id: id)
        }
    }
}
